package org.jeecg.modules.deepseek.tools;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.QueryWrapperBuilder;
import org.jeecg.common.util.QueryWrapperUtil;
import org.jeecg.common.util.deepseek.SqlQueryChecker;
import org.jeecg.modules.deepseek.pojo.Query.QueryWebTeammx;
import org.jeecg.modules.deepseek.pojo.SQLType;

import org.jeecg.modules.demo.wuyou.entity.WebTeammx;
import org.jeecg.modules.demo.wuyou.service.ISitemapService;
import org.jeecg.modules.demo.wuyou.service.IWebTeammxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class WebTeammxTool {

    @Autowired
    ISitemapService sitemapService;
    @Autowired
    private IWebTeammxService webTeammxService;

    public record QueryTeachersByName( @Schema(description = "老师姓名")String TeacherName) {

    }

    public record TableNameQuery( @Schema(description = "表名称")String TableName) {

    }
    @Schema(description = "查询老师信息所需字段，包括查询条件和排序条件。把查询SQL放在SelectSql字段中，只查询主要字段，不需要查询所有字段")
    public record QueryWebTeam(
            @Schema(description = "查询SQL")String selectSql,
            @Schema(description = "表结构：create table web_teammx\n" +
                    "(\n" +
                    "    id              varchar(100)            not null\n" +
                    "        primary key,\n" +
                    "    row_id          varchar(255)            null comment 'row_id',\n" +
                    "    team_id         varchar(100)            not null comment 'teamId',\n" +
                    "    imageurl        text                    null comment '图片地址',\n" +
                    "    TEAMMXNAME      varchar(50)  default '' null comment '团队人员名称',\n" +
                    "    AREAADVISER     varchar(50)  default '' null comment '地区顾问',\n" +
                    "    IMPORTAREA      varchar(50)  default '' null comment '负责地区',\n" +
                    "    SERVICEINFO     varchar(500) default '' null comment '咨询信息',\n" +
                    "    SEQ_ID          int                     not null comment '排序名称',\n" +
                    "    CASENUMBER      int                     null comment '案例总数',\n" +
                    "    PERSONNUMBER    int                     null comment '咨询认识',\n" +
                    "    WORKTIME        int                     null comment '工作年限',\n" +
                    "    jiangjie        text                    null comment '个人简介',\n" +
                    "    JAOYUBEIJ       varchar(500) default '' null comment '教育背景',\n" +
                    "    DICTIONARIES_ID varchar(100) default '' null comment '顾问级别'\n" +
                    ")\n" +
                    "    comment '团队信息明细';")String tableStructure,
            @Schema(description = "示例数据：  {\n" +
                    "    \"id\": \"12dcef1e-dde2-4a43-8d2f-7374d26e151b\",\n" +
                    "    \"row_id\": null,\n" +
                    "    \"team_id\": \"6d91fa775a3946ddacaed608fd45bc89\",\n" +
                    "    \"imageurl\": \"https://cdn.api2.51liuxue.com/scrm/2.png\",\n" +
                    "    \"TEAMMXNAME\": \"李老师\",\n" +
                    "    \"AREAADVISER\": \"英国,马来西亚,澳大利亚,新加坡,澳门,中国香港\",\n" +
                    "    \"IMPORTAREA\": \"北京市海淀区中关村大街19号新中关大厦B座北翼1005室\",\n" +
                    "    \"SERVICEINFO\": \"追求卓越，不断进步，成功之花为你盛开。\",\n" +
                    "    \"SEQ_ID\": 2,\n" +
                    "    \"CASENUMBER\": 1500,\n" +
                    "    \"PERSONNUMBER\": 322,\n" +
                    "    \"WORKTIME\": 6,\n" +
                    "    \"jiangjie\": \"凡是内心能够想到的，相信的都是可以达到的，过去和未来亦如此\",\n" +
                    "    \"JAOYUBEIJ\": \"追求卓越，不断进步，成功之花为你盛开。\",\n" +
                    "    \"DICTIONARIES_ID\": \"16b1dcf695d54e60b0daed74cd0d76aa\"\n" +
                    "  }")String sampleData
    ) {
    }





    @Bean
    @Description("根据条件生成SQL，查询老师（顾问）信息")
    public Function<QueryWebTeam, List<Map<String, Object>>> getWebTeamMxBySelected() {
        return request -> {
            try {
                String s = SqlQueryChecker.validateAndModifySql(request.selectSql, new String[]{"web_teammx"});
                log.debug("查询条件{}", s);
                List<Map<String, Object>> maps1 = sitemapService.GetByCondition(s, new HashMap<>());

//                List<Map<String, Object>> maps = sitemapService.GetByCondition("select db_field_name,db_field_txt\n" +
//                        "from onl_cgform_field left join onl_cgform_head oci on oci.id = onl_cgform_field.cgform_head_id\n" +
//                        "where oci.table_name in('web_teammx');", new HashMap<>());

//                Map<String, Object> resultMap = maps.stream()
//                        .collect(Collectors.toMap(
//                                map -> (String) map.get("db_field_name"),
//                                map -> (Object) map.get("db_field_txt"),
//                                (existingValue, newValue) -> existingValue
//                        ));
//                maps1.add(resultMap);
                return maps1;
            } catch (Exception e) {
                log.warn("查询失败{}", e.getMessage());
                return null;
            }
        };
    }
//    @Bean
//    @Description("根据条件生成SQL，查询老师（顾问）信息")
//    public Function<QueryWebTeam, List<WebTeammx>> getTeachersBySelectedTeam() {
//        return request -> {
//            try {
//                log.debug("查询条件{}", request.selectSql);
//                QueryWrapperBuilder.QueryWrapperWithPage<WebTeammx> build = QueryWrapperBuilder.build(request.selectSql, WebTeammx.class);
//                QueryWrapper<WebTeammx> queryWrapper = build.getQueryWrapper();
//                Page<WebTeammx> page = build.getPage();
//                return webTeammxService.list( page,queryWrapper);
//            } catch (Exception e) {
//                log.warn("查询失败{}", e.getMessage());
//                return null;
//            }
//        };
//    }


//    @Bean
//    @Description("根据条件查询老师（顾问）信息")
//    public Function<QueryWebTeammx, List<WebTeammx>> getTeachersByCondition() {
//        return request -> {
//            try {
//                //TODO 根据条件查询老师信息
//                log.debug("查询条件{}", request);
//                QueryWrapper<WebTeammx> webTeammxQueryWrapper = QueryWrapperUtil.buildQueryWrapper(request,WebTeammx.class);
//                return webTeammxService.list(webTeammxQueryWrapper);
//            } catch (Exception e) {
//                log.warn("查询失败{}", e.getMessage());
//                return null;
//            }
//        };
//    }
//    @Bean
//    @Description("查询老师数量")
//    public Supplier<Long> queryTeachersCount() {
//        return () -> {
//            try {
//                return webTeammxService.count();
//            } catch (Exception e) {
//                log.warn("查询失败{}", e.getMessage());
//                return null;
//            }
//        };
//    }

//    @Bean
//    @Description("查询老师姓名列表,目前有那些老师（顾问）")
//    public Supplier<List<String>> teachersNameList() {
//        return () -> {
//            try {
//                return webTeammxService.list().stream().map(WebTeammx::getTeammxname).toList();
//            } catch (Exception e) {
//                log.warn("查询失败{}", e.getMessage());
//                return null;
//            }
//        };
//    }
}
